package console;

/**
 * User: sachinh
 * Date: 25-Dec-2008
 * Time: 16:39:22
 */
public class Key {
    public static final int VK_LEFT = 0x25;
    public static final int VK_RIGHT = 0x27;
    public static final int VK_UP = 0x26;
    public static final int VK_DOWN = 0x28;
    public static final int VK_BKSPC = 0x08;
    public static final int VK_DEL = 0x2E;
    public static final int VK_HOME = 0x24;
    public static final int VK_END = 0x23;
    public static final int VK_ESC = 0x1B;
    public static final int VK_INSERT = 0x2D;

    public static final int CAPSLOCK_ON = 0x0080;
    public static final int ENHANCED_KEY= 0x0100;
    public static final int LEFT_ALT_PRESSED = 0x0002;
    public static final int LEFT_CTRL_PRESSED = 0x0008;
    public static final int NUMLOCK_ON = 0x0020;
    public static final int RIGHT_ALT_PRESSED = 0x0001;
    public static final int RIGHT_CTRL_PRESSED = 0x0004;
    public static final int SCROLLLOCK_ON = 0x0040;
    public static final int SHIFT_PRESSED = 0x0010;



    int code;
    int modifiers;
    char character;
    int charCode;

    public Key(int[] key) {
        code = key[0];
        // key[1] is scan code which we are not interested in
        character = (char)key[2];
        charCode = key[2];
        modifiers = key[3];
    }

    public int getCode() {
        return code;
    }

    public int getModifiers() {
        return modifiers;
    }

    public char getCharacter() {
        return character;
    }

    public boolean isCharacter() {
        switch(code) {
            case VK_LEFT:
            case VK_RIGHT:
            case VK_UP:
            case VK_DOWN:
            case VK_BKSPC:
            case VK_DEL:
            case VK_HOME:
            case VK_END:
            case VK_ESC:
            case VK_INSERT:
                return false;
        }
        return charCode != 0 && (!Character.isWhitespace(charCode) || charCode == ' ');
    }

    public boolean isControl() {
        return ((modifiers & LEFT_CTRL_PRESSED) | (modifiers & RIGHT_CTRL_PRESSED)) > 0;
    }

    public boolean isAlt() {
        return ((modifiers & LEFT_ALT_PRESSED) | (modifiers & RIGHT_ALT_PRESSED)) > 0;
    }

    public boolean isShift() {
        return (modifiers & SHIFT_PRESSED) > 0;
    }
}